package com.lqq.session;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;
import java.util.Enumeration;
import java.util.Map;

/**
 * @author lqq
 * @date 2019/3/4
 */
public class DispacherSessionImmpl implements HttpSession{
    private HttpSession session;
    private HttpServletRequest request;
    private HttpServletResponse response;
    private Map<String,Object> sessionMap=null;
    private SessionStore sessionStore=new SessionStore();
    private String sid;
    public DispacherSessionImmpl(){
    }
    public DispacherSessionImmpl(HttpSession session){
        this.session=session;
    }
    public DispacherSessionImmpl(HttpServletRequest request,HttpServletResponse response,String id){
        this.sid=id;
        this.request=request;
        this.response=response;
    }

    @Override
    public long getCreationTime() {
        return 0;
    }

    @Override
    public String getId() {
        return sid;
    }

    @Override
    public long getLastAccessedTime() {
        return 0;
    }

    @Override
    public ServletContext getServletContext() {
        return null;
    }

    @Override
    public void setMaxInactiveInterval(int i) {

    }

    @Override
    public int getMaxInactiveInterval() {
        return 0;
    }

    @Override
    public HttpSessionContext getSessionContext() {
        return null;
    }

    @Override
    public Object getAttribute(String name) {
        // 这个是获取你存储的信息的；
        if(sessionMap==null){
            sessionMap=sessionStore.getSession(this.getId());
        }
        return sessionMap.get(name);
    }

    @Override
    public Object getValue(String s) {
        return null;
    }

    @Override
    public Enumeration<String> getAttributeNames() {
        return null;
    }

    @Override
    public String[] getValueNames() {
        return new String[0];
    }

    @Override
    public void setAttribute(String name, Object value) {
        // 往session中存储数据的；这里就是redis中存储了；
        if(sessionMap==null){
            sessionMap=sessionStore.getSession(this.getId());
        }
        this.sessionMap.put(name, value);
        sessionStore.saveSession(this.getId(), sessionMap);
    }

    @Override
    public void putValue(String s, Object o) {

    }

    @Override
    public void removeAttribute(String name) {
        // 移除数据的方法；
        if(sessionMap==null){
            sessionMap=sessionStore.getSession(this.getId());
        }
        sessionMap.remove(name);
        sessionStore.removeSession(this.getId());
    }

    @Override
    public void removeValue(String s) {

    }


    @Override
    public void invalidate() {
        //删除用的；
        this.sessionMap.clear();
        sessionStore.removeSession(this.getId());

    }

    @Override
    public boolean isNew() {
        return false;
    }

}
